package com.example.course.service.impl;

import com.example.course.bean.Course;
import com.example.course.bean.Student;
import com.example.course.mapper.CourseMapper;
import com.example.course.mapper.StudentMapper;
import com.example.course.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Autowired
    private CourseMapper courseManager;
    @Override
    public Student getStudent(Student student) {
        return studentMapper.selectStudent(student);
    }

    @Override
    public List<Student> getStudents() {
        return studentMapper.selectStudents();
    }

    @Override
    public int updateStudent(Student student) {
        return studentMapper.updateStudent(student);
    }

    @Override
    public int deleteStudent(int id) {
        int i = studentMapper.deleteStudent(id);
        if (i==1){
            if (studentMapper.selectStudentByStudentId(id)!=null){
                int i1 = studentMapper.deleteCourseStudent2(id);
                if (i1!=0){
                    return 1;
                }else {
                    return 0;
                }
            }
        }

        return 0;
    }

    @Override
    public int insertStudent(Student student) {
        Student student1 = studentMapper.selectStudentByAccount(student.getAccount());
        if (student1 != null) {
            return 0;
        }
        return studentMapper.insertStudent(student);
    }

    @Override
    public List<Student> getStudentById( int teacherId,int courseId) {
        return studentMapper.selectStudentById(teacherId,courseId);
    }

    @Override
    public int applicationCourse(int courseId, int studentId,int teacherId) {
        int i = courseManager.selectCourseNumberById(courseId);
        if (i == 0) {
           return -1;
        }
        Course course = courseManager.selectCourseById(courseId);
        List<Student> students = courseManager.selectCourseByTime(course.getTime(), studentId);
        if (students.size() == 0){
            int i1=studentMapper.insertCourseByStudent(courseId,studentId,teacherId);
            if (i1 == 0){
                return 0;
            }else {
                return courseManager.updateCourseNumberById(courseId,-1);
            }
        }else {
            List<Course> students1 = courseManager.selectCourseByTimeAndBeginTimeOrEndTime(course.getTime(), studentId, course.getBeginTime(), course.getEndTime());
            System.out.println(4444);
            System.out.println(students1.size());
            System.out.println(8888);
            if (students1.size() != 0) {
                int f=studentMapper.insertCourseByStudent(courseId, studentId, teacherId);
                if(f!=0){
                    return courseManager.updateCourseNumberById(courseId,-1);
                }else {
                    return 0;
                }
            } else {
                return 0;
            }
        }
    }

    @Override
    public List<Course> getCourseByStudent(int studentId) {
        return courseManager.selectCourseByStudentId(studentId);
    }

    @Override
    public int deleteCourseByStudent(int studentId, int courseId) {
        int i=studentMapper.deleteCourseByStudent(studentId, courseId);
        if(i!=0){
            return courseManager.updateCourseNumberById(courseId,1);
        }
        return 0;
    }

    @Override
    public Student getStudentByStudentId(int id) {
        return studentMapper.selectStudentStudentById(id);
    }

    @Override
    public List<Student> getStudentByTeacherId(int teacherId) {
        return  studentMapper.selectStudentByTeacherId(teacherId);
    }
}
